#include "receivemsg.h"


receivemsglist::receivemsglist(QObject *parent)
	: QObject(parent)
{

}

receivemsglist::~receivemsglist()
{

}

void receivemsglist::AddMsg( const QString& ip,const QString& msg )
{
	msgDialog * msgDlg=MSGDLG.get(ip);
	if(!msgDlg->isVisible())
	{
		addToList(ip,msg);
	}
	else
	{
		msgDlg->setTextReceive(msg);
		msgDlg->activateWindow();
	}
	
}

void receivemsglist::addToList( const QString& ip,const QString& msg )
{
	_msgmap.insert(ip,msg);
	
	for (QLinkedList<QString>::iterator iter=_ip.begin ();iter!=_ip.end();iter++)
	{
		if (*iter==ip)
		{
			_ip.erase(iter);
		}
	}
	_ip.append(ip);

	emit setAlert(true);
}


void receivemsglist::postMsg()
{
	if (_ip.isEmpty())
	{
		return;
	}
	 QString ip=_ip.takeFirst();
	
	
	MsgMap::iterator iter=_msgmap.find(ip);
	MSGDLG.get(ip)->setTextReceive(iter.value());
	_msgmap.remove(ip);
	
	MSGDLG.get(ip)->show();
	MSGDLG.get(ip)->activateWindow();
	if (_ip.count()==0)
	{
		emit setAlert(false);
	}
	
}

